using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using UMC.WorkFlow;
using System.Collections.Specialized;
using System.Xml.XPath;
using UMC.Data;
using UMC.WorkFlow.Entities;

namespace UMC.WorkFlow
{
    /// <summary>
    /// 作业
    /// </summary>
    public class TaskManager
    {
        /// <summary>
        /// 保存作业结构
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="Username"></param>
        /// <returns></returns>
        public static bool Design(TaskInstance instance, string Username)
        {
            // var entity = UMC.WorkFlow.TaskRuntime.Database.ObjectEntity<TaskDesign>();
            // entity.Where.And(new TaskDesign { Id = instance.InstanceId });
            DataFactory.Instance().Put(new TaskDesign
            {
                Id = instance.InstanceId,
                Context = UMC.Data.JSON.Serialize(instance, true),
                UpdateDate = DateTime.Now,
                Name = instance.Text,
                Username = Username
            });
            return true;
        }
        /// <summary>
        /// 得到作业结构
        /// </summary>
        /// <returns></returns>
        public static string Design(ulong designId)
        {
            // var entity = UMC.WorkFlow.TaskRuntime.Database.ObjectEntity<TaskDesign>();
            var design = DataFactory.Instance().TaskDesign(designId);
            if (design != null)
            {
                return design.Context;
            }
            return null;

        }

        public static TaskInstance Form(ulong formId)
        {

            // var entity = UMC.WorkFlow.TaskRuntime.Database.ObjectEntity<TaskForm>();
            var instance = DataFactory.Instance().TaskForm(formId);// entity.Where.And().Equal(new TaskForm { TaskId = formId }).Entities.Single();
            if (instance == null)
            {
                return null;
            }
            else if (instance.DesignId.HasValue)
            {
                var inst = JSON.Deserialize<TaskInstance>(Design(instance.DesignId.Value));
                inst.TaskId = instance.TaskId.Value;
                inst.Text = instance.Caption;
                return inst;
            }
            return null;
        }

        public static TaskInstance Instance(ulong instanceId)
        {
            // var entity = UMC.WorkFlow.TaskRuntime.Database.ObjectEntity<TaskObSerialize>();
            var instance = DataFactory.Instance().TaskObSerialize(instanceId);// entity.Where.And().Equal(new TaskObSerialize { InstanceId = instanceId }).Entities.Single();
            if (instance == null)
            {
                return null;
            }
            else
            {
                return JSON.Deserialize<TaskInstance>(instance.SerializeData);
            }
        }

        public static void Instance(TaskInstance instance)
        {
            //  UMC.WorkFlow.TaskRuntime.Database
            // var entity = UMC.WorkFlow.TaskRuntime.Database.ObjectEntity<TaskObSerialize>();
            // entity.Where.And().Equal(new TaskObSerialize { InstanceId = instance.InstanceId });
           DataFactory.Instance().Put( new TaskObSerialize
            {
                Text = instance.Text,
                InstanceId = instance.InstanceId,
                SerializeData = JSON.Serialize(instance, true),
                Status = instance.Status,
                TaskId = instance.TaskId,
            });
            // entity.IFF(e => e.Update(enti) == 0, e => e.Insert(enti));
        }

    }
}
